package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import bean.AlarmInfo;
import util.DBUtil;

public class AlarmDao {
	PreparedStatement pstmt = null;
	DBUtil db=new DBUtil();
	Connection conn=db.open(); 
	ResultSet rs=null;
	//查询所有报警用户
	public  ArrayList<AlarmInfo> getAllAlarm(){
		ArrayList<AlarmInfo> list=new ArrayList<AlarmInfo>();
		String FIND="select * from alarmPeople";
		try {
			pstmt=conn.prepareStatement(FIND);
			rs=pstmt.executeQuery();
			while(rs.next()){
				String alarmPeople_id=rs.getString(1);
				String name=rs.getString(2);
				String phone=rs.getString(3);
				String beizhu=rs.getString(4);
				 AlarmInfo ainfo=new AlarmInfo();
				 
				 ainfo.setAlarmPeople_id(alarmPeople_id);
				 ainfo.setName(name);
				 ainfo.setPhone(phone);
			     ainfo.setBeizhu(beizhu);
				 
				 list.add(ainfo);				 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		return list;		
	}
   
	//对报警用户进行模糊查询
	public ArrayList<AlarmInfo> findByName(){
		ArrayList<AlarmInfo> list=new ArrayList<AlarmInfo>();
		String FINDBYNAME="select * from alarmPeople where name like ?";		
		try {
			pstmt=conn.prepareStatement(FINDBYNAME);
			rs=pstmt.executeQuery();
			while(rs.next()){
				String alarmPeople_id=rs.getString(1);
				String name=rs.getString(2);
				String phone=rs.getString(3);
				String beizhu=rs.getString(4);
				 AlarmInfo ainfo=new AlarmInfo();
				 
				 ainfo.setAlarmPeople_id(alarmPeople_id);
				 ainfo.setName(name);
				 ainfo.setPhone(phone);
			     ainfo.setBeizhu(beizhu);
				 
				 list.add(ainfo);	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		
		return list;
		
	}
	
	//添加报警用户
	public void addAlarmpeo(AlarmInfo ainfo){
		String INSERT="insert into alarmPeople(alarmPeople_id,name,telephone,mark) values(?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(INSERT);
			pstmt.setString(1, ainfo.getAlarmPeople_id());
			pstmt.setString(2, ainfo.getName());
			pstmt.setString(3, ainfo.getPhone());
			pstmt.setString(4, ainfo.getBeizhu());
			pstmt.executeUpdate();
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		
	}
	
	//删除报警用户
	public void deleAlarm(String id){
		String DELETE="delete from alarmPeople where alarmPeople_id='"+id+"'";
		try {
			pstmt=conn.prepareStatement(DELETE);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
	}
}
